<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
     <%@ page import="java.sql.*" %>
    <%@ page import="java.util.*"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK REL=stylesheet HREF="css/111.css" TYPE="text/css">
<title>二次搜索</title>
</head>
<body>
<form method="post" action="R2.jsp" AUTOCOMPLETE="OFF">
	         <div style="float:left; font-size:20px; top:10px; position:relative;">
	        	 请选择搜索的方式：
	            <select name="way" style="width:60px; height:30px;">
	            <option value="school_num" selected="selected">学校</option>
	            <option value="user1" >用户</option>
	            <option value="topic" >话题</option>
	            <option value="post"  >帖子</option>
	            </select>
	          
	         </div>
	         <input type="text" name="research" size="60" style="height:30px; font-size:60%"/>
	         <button type="submit" style="font-size:20px;">搜索</button>
	         </form>
	         </div>
	         <%
	         Connection conn=null;
	         request.setCharacterEncoding("utf-8");
	         String way=request.getParameter("way");
	    	 String research=request.getParameter("research");
	    	 //String research=new String(request.getParameter("research").getBytes("GBK"),"utf-8");
	    	
	    	 System.out.println(way);
	    	 System.out.println(research);
	         out.print("<h3>"+research+"搜索结果</h3>");
	       
	         int pageSize = 10;//每页显示记录数
	         int listStep = 20;//最多显示分页页数
	         int pages = 1;    //默认显示第一页
	         int rowCount;        //记录总数
	         int pageCount;      //总页数
	        
	         //取得待显示页码
	         if (request.getParameter("pages") != null) {
	                pages = Integer.parseInt(request.getParameter("pages")); 
	                 }
	         int recordBegin=(pages-1)*pageSize  ;//每页显示的一个记录
	         int listBegin = (pages- (int) Math.ceil((double) listStep / 2));//从第几页开始显示分页信息
	         int listend = pages + listStep/2;//分页信息显示到第几页
	       
	         try
	         {
	            Class.forName("com.mysql.jdbc.Driver");
	            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/school1130","root","root");
	            Statement stmt=conn.createStatement();
	            Statement stmt1=conn.createStatement();
	            Statement stmt2=conn.createStatement();
	            Statement stmt3=conn.createStatement();
	            Statement stmt4=conn.createStatement();
	            Statement stmt5=conn.createStatement();
	            ResultSet ra=null;
	            ResultSet rb=null;
	       	 ResultSet rc=null;
	       	 ResultSet rd=null;
	       	 ResultSet re=null;
	         ResultSet rf=null;
	       	 
	       	 ResultSet sqlRst=null;
	            List<String> str_list = new ArrayList<String>(); 
	            if(way.equals("school_num")){
	                ra=stmt.executeQuery("select  school_num  from school  where  name like '%"+research+"%'");
	                sqlRst=stmt5.executeQuery("select count(school_num) from school  where  name like '%"+research+"%'");
	           }else if(way.equals("user1")){ ra=stmt.executeQuery("select  account  from user1  where  nickname like '%"+research+"%'");
	                 sqlRst=stmt5.executeQuery("select  count(account)  from user1  where  nickname like '%"+research+"%'");}
	            else if(way.equals("topic")){ ra=stmt.executeQuery("select  topic_num  from topic  where content like '%"+research+"%'");
                     sqlRst=stmt5.executeQuery("select  count(topic_num)  from topic  where content like '%"+research+"%'");}
	            else if(way.equals("post")){ ra=stmt.executeQuery("select  post_num  from post  where content like '%"+research+"%' or theme like '%"+research+"%'");
                sqlRst=stmt5.executeQuery("select  count(post_num)  from post  where content like '%"+research+"%'  or theme like '%"+research+"%'"); }
	            sqlRst.next();
	            rowCount=sqlRst.getInt(1);
	            if(rowCount==0){ out.print("<h5>搜索不到该关键词，请尝试重新搜索<h5>");}
	            else{out.print("<h3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "+rowCount+"</h3>");}
	            while(ra.next()){
	           	  str_list.add(ra.getString(1));
	           	  request.setAttribute("str_list",str_list);
	           	 } 
	                List  ps=(List)request.getAttribute("str_list");
	                
	              //计算总页数
	                pageCount=(rowCount+pageSize-1)/pageSize;
	              //调整待显示的页码
	              if(pages>pageCount)  pages=pageCount; 
	              if(pageCount>0){ra.absolute((pages-1)*pageSize+1);}
	              
	              if (listBegin < 1) {
	           	   listBegin = 1;
	              }
	             
	              if (listend > pageCount) {
	                listend = pageCount + 1;
	              } 
	              if(rowCount!=0){
	                %>
	                 <div id="fashionlist">
         <% 
    	 
    		 
    		 if(way.equals("school_num")){
    			 for(int i=0;i<str_list.size() ;i++)
    	    	 {	  //out.print(ps.get(i));
    			 String image_url="images/"+ps.get(i)+".jpg";
    			  rb=stmt1.executeQuery("select count(*) from school where school_num="+ps.get(i)   );
    		      rc=stmt2.executeQuery("select count(*) from post, user1 where(user1.account=post.account&&user1.school_num ="+ps.get(i)+" )")  ;
    		      rd=stmt3.executeQuery("select introduction from school  where school_num=" +ps.get(i)   );
    		      re=stmt4.executeQuery("select name from school  where school_num=" +ps.get(i)   );
    		      %>
    			    <div id="fashion">
    			    <div id="image"  style="float:left; " >
    			    <a href='which-uni.jsp?num=<%=ps.get(i)%>&iPage=1'><image src=<%=image_url %> height="100px" width="100px"></image></a></div>
    			    <div id="school_information" >
    			    <% 
    			    while(re.next()){
    				    out.print("<h4><a href='which-uni.jsp?num="+ps.get(i)+"&iPage=1' > ");
    				    //System.out.println("<a href='which-uni.jsp?num="+ps.get(i)+"&iPage=1' target='_parent'>");
    				    out.print(re.getString(1));
    				    out.print("</a></h4> ");}
    				     
    				    out.print("<a href=''>关注量：</a><a href=''> ");
    				     while(rb.next()){
    				    Integer a= rb.getInt(1);
    				     if (a==0) {out.println("0");}
    				     else{out.println(""+a);}}
    				    out.print("</a><br/> ");
    				    
    				    out.print("<a href=''>帖子数：</a><a href=''> ");
    				    while(rc.next()){
    					    Integer a= rc.getInt(1);
    					     if (a==0) {out.println("0");}
    					     else{out.println(""+a);}}
    					    out.print("</a><br/><a href=''> "); 
    				    
    				    while(rd.next()){  
    				    String a=rd.getString(1);
    				    if(a==null)  {out.print("");}
    				    else{
    				    out.print(a);}
    				    out.print("</a>");}
    				    %>
        				</div></div>  
        				    <% }
    				   
    	 }
    		 if(way.equals("user1")){
    			 for(int i=0;i<str_list.size() ;i++)
    	    	 {	  //out.print(ps.get(i));   
   		      rb=stmt1.executeQuery("select nickname from user1 where account='"+ps.get(i)+"'"   );
   		      rc=stmt2.executeQuery("select count(*) from user_user where (account1='"+ps.get(i)+" ') ")  ;
   		      rd=stmt3.executeQuery("select count(*) from post  where account='" +ps.get(i)+"'");
   		      re=stmt4.executeQuery("select image from user1  where account='" +ps.get(i)+"'"   );
   		      %>
   			    <div id="fashion">
   			   <div id="image"  style="float:left;" > 
   		    <% 
   		    
   		    
   		    while(re!=null&&re.next()){
   		    	String image_url=re.getString(1);
   		     
   		    //	out.print("<img src="+image_url+"> ");
   		    %>	
   		       <a href=''><image src=<%=image_url %> height="100px" width="100px"></image></a></div> <%} %>
   		       <div id="school_information" >
   		      <%  
   		   
   		    
   		    while(rb.next()){
   		    out.print("<h4><a href=''>");
   		    out.print(rb.getString(1));
   		    
   		      out.print("</a></h4>");}
   		     out.print("<a href=''> 关注好友数：</a><a href=''> ");
   		      
   		     while(rc.next()){
   		    Integer a= rc.getInt(1);
   		     if (a==0) {out.println("0");}
   		     else{out.println(""+a);}}
   		    out.print("</a><br/> ");
   		    out.print(" <a href=''>帖子数：</a><a href=''> ");
   		   
   		    while(rd.next()){
   		     Integer a= rd.getInt(1);
   		     if (a==0) {out.println("0");}
   		     else{out.println(""+a);}
   		     out.print("</a>");}%>
			  </div></div>
			  <%  }
    			  
    		 }
    		 if(way.equals("topic")){
    			 for(int i=0;i<str_list.size() ;i++)
    	    	 {	  //out.print(ps.get(i));
    		  
    			 String image_url="images/t"+ps.get(i)+".jpg";
    	    	 
    				   
    			      rb=stmt1.executeQuery("select content from topic where topic_num="+ps.get(i) );
    			      rc=stmt2.executeQuery("select count(*) from post where (topic_num="+ps.get(i)+")");
    			      rd=stmt3.executeQuery("select count(distinct account) from post  where(topic_num="+ps.get(i)+")"); 
    			      %>
    				    <div id="fashion">
    				    <div id="image"  style="float:left; " >
    				    <a href=''><image src=<%=image_url %> height="100px" width="100px"></image></a></div>
    				    <div id="school_information" >
    				    <% 
    			    while(rb.next()){
    			    out.print("<h4><a href='OneTopic.jsp?topicContent="+rb.getString(1)+"&iPage=1' target='_parent'>");
    			    out.print(rb.getString(1));
    			    out.print("</a></h4> ");}
    			    
    			     out.print("<a href=''> 参与人数：</a><a href=''> ");
    			    while(rd.next()){
    			    Integer a= rd.getInt(1);
    			     if (a==0) {out.println("0");}
    			     else{out.println(""+a);}}
    			    out.print("</a><br/>");
    			    
    			    out.print("<a href=''> 帖子数：&nbsp;&nbsp; </a><a href=''>");
    			   
    			    while(rc.next()){
    			     Integer a= rc.getInt(1);
    			     if (a==0) {out.println("0");}
    			     else{out.println(""+a);}
    			     out.print("</a>");} %>
       			  </div></div>
       			  <% }
    			 
    		 }	
    		 if(way.equals("post")){
    			 for(int i=0;i<str_list.size() ;i++)
    				 
    				 
    	    	 {	  out.print(ps.get(i)); 
   	    		   	  rb=stmt1.executeQuery("SELECT content FROM topic WHERE topic_num IN(SELECT topic_num  FROM post WHERE post_num="+ps.get(i)+")" );
     			      rc=stmt2.executeQuery(" SELECT theme FROM post WHERE post_num="+ps.get(i));
     			      rd=stmt3.executeQuery("SELECT nickname FROM user1 WHERE account IN(SELECT account  FROM post WHERE post_num="+ps.get(i)+")"); 
     			      re=stmt4.executeQuery("SELECT content FROM post WHERE   post_num="+ps.get(i));
     			      rf=stmt5.executeQuery("select imageurl from post where post_num="+ps.get(i));
     			      %>
    				 <table width="800" border="0" cellspacing="0" cellpadding="0" >
    				  	<tr>
    				    	<td width="500" rowspan="2">
    				    		 <%     while(rb.next()){
    				    			    out.print("<td><a href=' ' ");
    				    			    out.print("rb"+rb.getString(1));
    				    			    System.out.println(rb.getString(1));
    				    			    out.print("</a></td>");}
    				    		 
    				    			    
    				    		       while(rc.next()){
    				    		    	   out.print("<h4>");
    				    		    	   out.print(rc.getString(1));
    				    		    	   out.print("</h4></tr> ");}
    				    		       out.print("<tr><td width='200' style='font-size:10px;COLOR: #c0c0c0;'> 作者：&nbsp;&nbsp;"  ); 
    				    		       while(rd.next()){
    				    		    	   out.print("<h4><a href=' /xiaonei1227/homepage2.jsp?nickname="+rd.getString(1)+"' ");
    				    		    	   out.print(rc.getString(1));
    				    		    	   out.print("</a></h4></td></tr> ");}
    				    			    
    				    			    out.print("<tr>  内容：&nbsp;&nbsp;"  ); 
    				    			    while(re.next()){
     				    		    	   out.print(" <a href=' /xiaonei1227/homepage2.jsp?nickname="+re.getString(1)+"' ");
     				    		    	   out.print(re.getString(1));
     				    		    	   out.print("</h4> </tr>");}%>
    				    			    <div id="image"  style="float:left;" > 
    				    	   		    <% 
    				    	   		    
    				    	   		    
    				    	   		    while(rf!=null&&rf.next()){
    				    	   		    	String image_url=rf.getString(1);
    				    	   		     
    				    	   		    //	out.print("<img src="+image_url+"> ");
    				    	   		    %>	
    				    	   		       <a href=''><image src=<%=image_url %> height="100px" width="100px"></image></a></div> <%} 
    				    			    
    				    	
    	    
    		 }
    	 }
	              } %>
			   
		 </table>
		    	  
		    	  
 

		 
 
  <% 	
  out.print("<div class='change'><ul>");
     //<显示上一页         
      if (pages> 1) {
                    
                   
    	              out.print(  
                    
                     " <li> <a href=?pages=" + (pages - 1) + ">上一页</a></li>");
                }//>显示上一页
                //<显示分页码
                for (int j = listBegin; j < listend; j++) {
                    if (j != pages) {//如果j不等于当前页
                        out.print(
                        "<li><a href=?pages=" + j + ">" + j + "</a></li>");
                    } else {
                        out.print("<li>" + j + "</li>");
                    }
                }//显示分页码>
                //<显示下一页
                if (pages !=pageCount) {
                    out.print(
                    "<li><a href=?pages=" + (pages+ 1) + ">下一页</a></li>");
                }
                out.print("</ul></div>");
                 
		         
   

 
  ra.close();
  stmt.close();
  rb.close();
  stmt1.close();
  rc.close();
  stmt2.close();
  rd.close();
  stmt3.close();
  sqlRst.close();
  stmt4.close();
  conn.close();
  }
  catch(Exception e)
  {
     e.printStackTrace();  }
%>
</body>
</html>